package com.goodfather.textbook.youdao.pad.util;

import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.goodfather.textbook.youdao.pad.R;
import com.goodfather.textbook.youdao.pad.bean.BookRatioTag;
import com.goodfather.textbook.youdao.pad.weight.AudioImageView;

public class PageImgHelper {

    public static void loadImage(final int pageNo, String curImgUrl, final ImageView curShowImgView, final ViewGroup parentView) {
        Glide.with(C.get()).asBitmap().apply(new RequestOptions().placeholder(R.drawable.teach_default)
                .priority(Priority.NORMAL).dontAnimate())
                .load(curImgUrl)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        float wRatio = (float) curShowImgView.getWidth() / width;
                        float hRatio = (float) curShowImgView.getHeight() / height;
                        curShowImgView.setImageBitmap(bitmap);
                        parentView.setTag(new BookRatioTag(wRatio, hRatio, pageNo));
                    }
                });

    }

    public static void loadImage(final int pageNo, String curImgUrl, final AudioImageView curShowImgView, final ViewGroup parent, final ImageRatioCallBack callBack) {
        Glide.with(C.get()).asBitmap().apply(new RequestOptions().placeholder(R.drawable.teach_default)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.teach_default)
                .priority(Priority.NORMAL).dontAnimate())
                .load(curImgUrl)
                .into(new SimpleTarget<Bitmap>() {
                          @Override
                          public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {

                              int width = bitmap.getWidth();
                              int height = bitmap.getHeight();
                              float wRatio = (float) curShowImgView.getWidth() / width;
                              float hRatio = (float) curShowImgView.getHeight() / height;
                              curShowImgView.setImageBitmap(bitmap);
                              parent.setTag(pageNo);
                              callBack.imageRatio(wRatio,hRatio);

                          }
                      });
    }

    /**
     * 图片加载完成
     */
    public interface ImageRatioCallBack {

        void  imageRatio(float wRatio, float hRatio);//图片比率
    }
}
